/*
 * @Description: 光盘检测 - 真实性检测
 * @Author: Rfan
 * @Date: 2022-10-10 14:19:09
 * @LastEditTime: 2022-10-21 18:11:05
 */

import { forwardRef, useEffect, useImperativeHandle, useState } from 'react';
import { Button, Checkbox, Space } from 'antd';
import cs from 'classnames';
import classnames from 'classnames/bind';
import { useAddPlanDialogContext } from '../../../contexts/AddPlanDialogContext';
import { CheckProps, EnumAuthenticityRuleType } from '../../../../../../dictionaries';
import type { IPropsRule } from '../../../../../../interface';
import styles from '../../../styles/entryPanes.module.scss';
import mainStyles from '../../../styles/main.module.scss';
import { isNullObj } from '@/utils';

const cx = classnames.bind(styles);
const mainCx = classnames.bind(mainStyles);
// CD真实性规则列表
const CDAuthList = [9, 10];

// CD真实性规则列表类型
type TRuleList = IPropsRule<EnumAuthenticityRuleType>[];

// CD真实性完整类型
interface IAuthenticityFullRules {
  ruleType: CheckProps.Authenticity;
  rules: TRuleList;
}

const CDAuthentication = (props: any, ref: any) => {
  const {
    currentRuleInfo,
    getPropertyRules,
    selectAllRules,
    selectInverseRules,
    getRuleState,
    setRuleState
  } = useAddPlanDialogContext();

  // 规则列表
  const [ruleList, setRuleList] = useState<TRuleList>([]);

  useImperativeHandle(ref, () => ({
    save: () => {
      const fullRules: IAuthenticityFullRules = {
        ruleType: CheckProps.Authenticity,
        rules: ruleList
      };
      return fullRules;
    }
  }));

  /**
   * @description: 获取到规则后回写
   */
  useEffect(() => {
    if (currentRuleInfo && !isNullObj(currentRuleInfo)) {
      const ruleObj = getPropertyRules(CheckProps.Authenticity);
      setRuleList(ruleObj?.rules || []);
    }
  }, [currentRuleInfo]);

  return (
    <div className={cs(mainCx('property-wrap'), cx('property-authenticity'))}>
      <Space className={mainCx('property--title-wrap')} align="center">
        <h6 className={mainCx('property--title')}>光盘格式包真实性</h6>
        <div className={mainCx('select-btn')}>
          <Button
            type="text"
            size="small"
            onClick={() => {
              setRuleList(selectAllRules(CDAuthList));
            }}
          >
            全选
          </Button>{' '}
          /{' '}
          <Button
            type="text"
            size="small"
            onClick={() => {
              setRuleList(selectInverseRules(CDAuthList, ruleList));
            }}
          >
            反选
          </Button>
        </div>
      </Space>

      <Space direction="vertical">
        <Checkbox
          checked={getRuleState(EnumAuthenticityRuleType.DiskStandardized, ruleList)}
          onChange={(evt) => {
            setRuleState(
              evt.target.checked,
              EnumAuthenticityRuleType.DiskStandardized,
              ruleList,
              setRuleList
            );
          }}
        >
          说明文件和目录文件规范性检测
        </Checkbox>
        <Checkbox
          checked={getRuleState(EnumAuthenticityRuleType.DiskCatalogueStandardized, ruleList)}
          onChange={(evt) => {
            setRuleState(
              evt.target.checked,
              EnumAuthenticityRuleType.DiskCatalogueStandardized,
              ruleList,
              setRuleList
            );
          }}
        >
          信息包目录结构规范性检测
        </Checkbox>
      </Space>
    </div>
  );
};

export default forwardRef(CDAuthentication);
